package com.wind.cqboot.plugin;

import java.util.Random;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wind.cqboot.config.DiaryCate;
import com.wind.cqboot.consts.AlApiConst;
import com.wind.cqboot.utils.HttpUtil;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;

/**
 * <p>File：InterestingPlugin.java</p>
 * <p>Title: 有趣接口合集</p>
 * <p>Description: 包含渣男语录、一言、毒鸡汤、舔狗日记</p>
 * <p>Copyright: Copyright (c) 2020 2020年3月26日 下午3:12:40</p>
 * <p>Company: zmrit.com </p>
 * @author zmr
 * @version 1.0
 */
@Component
public class InterestingPlugin extends CQPlugin
{
    @Value("${alapi.token}")
    private String ALAPI_TOKEN;

    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event)
    {
        String msg = event.getMessage();
        Long groupId = event.getGroupId();
        String content = "";
        if (msg.equals("渣男语录"))
        {
            content = buildZhanan();
        }
        else if (msg.equals("毒鸡汤"))
        {
            content = buildDujitang();
        }
        else if (msg.equals("一言"))
        {
            content = buildYiyan();
        }
        else if (msg.equals("舔狗日记"))
        {
            // content = buildDiary(DiaryCate.TIANGOU);
            int i = new Random().nextInt(2);
            content = i % 2 == 0 ? buildDiary() : buildXjDiary();
        }
        else if (msg.equals("吟诗一首"))
        {
            content = buildShici();
        }
        else if (msg.contains("藏头诗"))
        {
            String[] msgs = msg.split(" ");
            if (msgs.length == 2 && msgs[1].length() == 4)
            {
                content = buildPoem(msgs[1]);
            }
        }
        else if (msg.contains("对对子"))
        {
            String[] msgs = msg.split(" ");
            if (msgs.length == 2)
            {
                content = buildCouplet(msgs[1]);
            }
        }
        // else if (msg.equals("女神日记"))
        // {
        // content = buildDiary(DiaryCate.NVSHEN);
        // }
        // else if (msg.equals("保安日记"))
        // {
        // content = buildDiary(DiaryCate.BAOAN);
        // }
        // else if (msg.equals("业主日记"))
        // {
        // content = buildDiary(DiaryCate.YEZHU);
        // }
        if (StrUtil.isNotBlank(content))
        {
            cq.sendGroupMsg(groupId, content, false);
            return MESSAGE_BLOCK;
        }
        // 继续执行下一个插件
        return MESSAGE_IGNORE;
    }

    // 渣男语录
    private static String buildZhanan()
    {
        // WebSite/{n} n是整数，表示返回的条数，不写返回一条
        String result = HttpUtil.sendGet("https://api.lovelive.tools/api/SweetNothings/WebSite");
        if (StrUtil.isNotBlank(result))
        {
            StringBuffer sb = new StringBuffer();
            String text = "";
            sb.append("渣男：说话的艺术").append("\r\n");
            sb.append(result);
            text = sb.toString();
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    // 一言
    private static String buildYiyan()
    {
        String result = HttpUtil.sendGet("https://api.daidr.me/apis/hitokoto.json");
        if (StrUtil.isNotBlank(result))
        {
            JsonObject data = JsonParser.parseString(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            sb.append("一言").append("\r\n");
            sb.append(data.get("content").getAsString()).append("\r\n");
            sb.append("--来自：").append(data.get("from").getAsString());
            text = sb.toString();
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    // 毒鸡汤
    private static String buildDujitang()
    {
        String result = HttpUtil.sendGet("https://api.daidr.me/apis/poisonous.json");
        if (StrUtil.isNotBlank(result))
        {
            JsonObject data = JsonParser.parseString(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            sb.append("干了这碗毒鸡汤：").append("\r\n");
            sb.append(data.get("content").getAsString());
            text = sb.toString();
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    // 舔狗日记
    @SuppressWarnings("unused")
    private static String buildDiary(DiaryCate cate)
    {
        String result = HttpUtil.sendGet("http://api.diary.zmrit.com/random/" + cate.getCate());
        if (StrUtil.isNotBlank(result))
        {
            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            if (json.get("code").getAsInt() == 0)
            {
                JsonObject data = json.get("data").getAsJsonObject();
                sb.append(cate.getTitle()).append("\r\n");
                sb.append("日期：").append(data.get("date").getAsString()).append("\r\n");
                sb.append("天气：").append(data.get("weather").getAsString()).append("\r\n");
                sb.append(data.get("content").getAsString());
                text = sb.toString();
            }
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    // 舔狗日记
    private static String buildDiary()
    {
        String result = HttpUtil.sendGet("https://www.somekey.cn/tiangou/random.php");
        if (StrUtil.isNotBlank(result))
        {
            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            if (json.get("code").getAsInt() == 1)
            {
                JsonObject data = json.get("data").getAsJsonObject();
                sb.append("舔狗日记\r\n");
                sb.append("日期：").append(data.get("date").getAsString()).append("\r\n");
                sb.append("天气：").append(data.get("weather").getAsString()).append("\r\n");
                sb.append(data.get("content").getAsString());
                text = sb.toString();
            }
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    // 舔狗日记
    private String buildXjDiary()
    {
        String url = StrUtil.format(AlApiConst.URL_DOG, ALAPI_TOKEN);
        String result = HttpUtil.sendGet(url);
        if (StrUtil.isNotBlank(result))
        {
            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            if (json.get("code").getAsInt() == 200)
            {
                JsonObject data = json.get("data").getAsJsonObject();
                sb.append("舔狗日记\r\n");
                sb.append("日期：").append(DateUtil.today()).append("\r\n");
                // sb.append("天气：").append(data.get("weather").getAsString()).append("\r\n");
                sb.append(data.get("content").getAsString());
                text = sb.toString();
            }
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    /**
     * @param string
     * @return
     * @author zmr
     */
    private String buildPoem(String keyword)
    {
        String result = HttpUtil.sendGet(StrUtil.format(AlApiConst.URL_POEM, ALAPI_TOKEN, keyword));
        if (StrUtil.isNotBlank(result))
        {
            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            if (json.get("code").getAsInt() == 200)
            {
                JsonObject data = json.get("data").getAsJsonObject();
                sb.append(data.get("poem").getAsString());
                text = sb.toString();
            }
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    private String buildShici()
    {
        String result = HttpUtil.sendGet(StrUtil.format(AlApiConst.URL_SHICI, ALAPI_TOKEN));
        if (StrUtil.isNotBlank(result))
        {
            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            if (json.get("code").getAsInt() == 200)
            {
                JsonObject data = json.get("data").getAsJsonObject();
                sb.append("作者：").append(data.get("author").getAsString()).append("\r\n");
                sb.append("类型：").append(data.get("category").getAsString()).append("\r\n");
                sb.append("诗词题：").append(data.get("origin").getAsString()).append("\r\n");
                sb.append("诗词内容：").append(data.get("content").getAsString()).append("\r\n");
                text = sb.toString();
            }
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }

    private String buildCouplet(String keyword)
    {
        String result = HttpUtil.sendGet(StrUtil.format(AlApiConst.URL_COUPLET, ALAPI_TOKEN, keyword));
        if (StrUtil.isNotBlank(result))
        {
            JsonObject json = JsonParser.parseString(result).getAsJsonObject();
            StringBuffer sb = new StringBuffer();
            String text = "";
            if (json.get("code").getAsInt() == 200)
            {
                JsonObject data = json.get("data").getAsJsonObject();
                sb.append("上联：").append(data.get("keyword").getAsString()).append("\r\n");
                sb.append("下联：").append(data.get("text").getAsString()).append("\r\n");
                text = sb.toString();
            }
            return text;
        }
        return "机器人暂时失联了，请稍后再试";
    }
}
